import 'package:changxindianzishangwu/dao/title_dao.dart';
import 'package:changxindianzishangwu/model/titles_list.dart';
import 'package:changxindianzishangwu/provider/nickname.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_callback.dart';
import 'package:tencent_im_sdk_plugin/tencent_im_sdk_plugin.dart';

class SetFriendNicknamePage extends StatefulWidget {
  String userId;
  String name;
  SetFriendNicknamePage({Key key, this.userId, this.name}) : super(key: key);

  @override
  _SetFriendNicknamePageState createState() => _SetFriendNicknamePageState();
}

class _SetFriendNicknamePageState extends State<SetFriendNicknamePage> {
  var nickName = "";
  TextEditingController textEditingController = TextEditingController();

  @override
  void initState() {
    super.initState();
    textEditingController.text = widget.name;
    getData();
  }

  TitleList titleList;
  List<Widget> itemWidget = [];
  getData() async {
    itemWidget = [];
    titleList = await TitleDao.selectTitle();
    if (titleList.resultCode == 1) {
      titleList.data.forEach((element) {
        itemWidget.add(InkWell(
          onTap: () {},
          child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.all(5.w),
            padding: EdgeInsets.all(5.w),
            width: 40.w,
            height: 30,
            decoration: BoxDecoration(
                border: Border.all(
                  color: ZdColor.rgba(0, 220, 169, 1),
                ),
                borderRadius: BorderRadius.circular(12)),
            child: Text(
              element.groupName,
              style: TextStyle(
                  fontSize: 11.sp, color: ZdColor.rgba(0, 220, 169, 1)),
              maxLines: 1,
              textAlign: TextAlign.center,
            ),
          ),
        ));
      });
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(
          "设置备注和标签",
          style: TextStyle(
            fontSize: ScreenUtil().setSp(16),
          ),
        ),
        centerTitle: true,
        actions: [
          InkWell(
            onTap: () async {
              EasyLoading.show(status: "修改中...");
              V2TimCallback res = await TencentImSDKPlugin.v2TIMManager
                  .getFriendshipManager()
                  .setFriendInfo(
                    userID: widget.userId,
                    friendRemark: nickName,
                  );
              if (res.code == 0) {
                print("succcess");
                if (nickName == null ||
                    nickName == '' ||
                    nickName.trim() == '') {
                  Provider.of<NickName>(context, listen: false)
                      .add(widget.name);
                  EasyLoading.dismiss();
                  Get.back();
                } else {
                  Provider.of<NickName>(context, listen: false).add(nickName);
                  EasyLoading.dismiss();
                  Get.back();
                }
              } else {
                print(res);
              }
            },
            child: Container(
              alignment: Alignment.center,
              margin: EdgeInsets.only(top: 11, bottom: 11, right: 15),
              height: 10,
              width: ScreenUtil().setWidth(60),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(6),
                color: Color.fromRGBO(0, 220, 169, 1),
              ),
              child: Text(
                "完成",
                style: TextStyle(
                    color: Colors.white, fontSize: ScreenUtil().setSp(14)),
              ),
            ),
          )
        ],
      ),
      body: Container(
        child: Column(
          children: [
            Container(
              color: Colors.white,
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 15.w),
              child: Text(
                "备注名",
                style: TextStyle(
                    fontSize: 12.sp, color: ZdColor.rgba(193, 193, 193, 1)),
              ),
            ),
            Container(
              color: Colors.white,
              height: 60,
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 15.w),
              child: TextField(
                controller: textEditingController,
                onChanged: (value) {
                  nickName = value;
                  print(value);
                },
                keyboardType: TextInputType.name,
                style: TextStyle(fontSize: 15.sp),
                cursorColor: Colors.greenAccent,
                decoration: InputDecoration(
                  border: InputBorder.none,
                ),
              ),
            ),
            SizedBox(
              height: 10,
            ),
            Container(
              color: Colors.white,
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 15.w, top: 6),
              child: Text(
                "标签",
                style: TextStyle(
                    fontSize: 12.sp, color: ZdColor.rgba(193, 193, 193, 1)),
              ),
            ),
            Container(
                constraints: BoxConstraints(minHeight: 60),
                color: Colors.white,
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(left: 15.w),
                child: Wrap(
                  children: itemWidget,
                ))
          ],
        ),
      ),
    );
  }
}
